//join.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var uid = 0;
var token = '';
var artType = 15; //艺术家类型 2
Page({
  data: {
      uid:0,  //当前登录用户Id
      token:'', //post请求时，当前登录用户token
      authData: null, //未认证
      imgDomain: app.globalData.imgDomain,
      hz_tequan: "1、加V图标，身份更尊贵，信誉值更高。\n2、通讯录顶部推荐1个月。曝光率增加300倍。\n3、发布的动态显示权重更高。\n4、不限制交换名片的数量。\n5、每天申请交换名片的次数提高到100次。\n6、每天直联次数息提高到30次。\n7、更多特权陆续增加中。"
  },
  
  onLoad: function (options) {
    var that = this;
    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function(res){
      //更新数据
      if (res && res != -1) {
      that.setData({
        uid:res.uid,
        token: res.token
      })
      uid = res.uid;
      token = res.token;
      //获取认证状态
      that.getAuth(uid, token);
      }
    }, inviteuid);
  },

  //获取用户认证状态
  getAuth: function (uid, token) {
      var that = this;  
      var privatekey = utilMd5.hexMD5(uid+'getUserAuth'+token);
    util.api({
        data:{
            publickey:util.publiccode,
            ac:"getUserAuth",
            uid:uid,
            type: artType,
            privatekey: privatekey
        },
        method:'GET',
        success:function(responseData){
            var data = responseData.data;
            console.log("UserAuth:", data);    
            if(data && data.res != false && data.realname) {
              //更新数据
              that.setData({
                authData:data
              });
            } else {
               that.setData({
                authData:null
              });
            }
        }
    });   
  },

  //填写资料提交
  joinSubmit: function (e) {
    var that = this;
    console.log("formId ", e.detail.formId);
    app.saveFormId(e.detail.formId);//保存fromId
    
    var nextUrl = '/pages/sign-pay/sign-pay';
    if (that.data.authData){
      app.dialog('提交认证成功', 'success', function () { wx.redirectTo({ url: nextUrl }) });
      return;
    }

      var uid = e.detail.value.uid;
      var name = e.detail.value.name;
      var token = e.detail.value.token;
      var weixin = e.detail.value.weixin;
      var mobile = e.detail.value.mobile;
      var privatekey = utilMd5.hexMD5(uid+'userauth'+token);
      if(!name || !weixin || !mobile) {
        app.dialog('请填写完整资料', 'loading');
          return;
      }
    util.api({
        data:{
            publickey:util.publiccode,
            ac:"userauth",
            uid:uid,
            name: name,
            weixin:weixin,
            phone:mobile,
            type: artType,
            privatekey: privatekey
        },
        method:'POST',
        success:function(responseData){
            var data = responseData.data;
            if(data.res === true) {
              app.dialog('提交认证成功', 'success', function () { wx.redirectTo({url: nextUrl})});
            } else {
                app.dialog('提交认证失败', 'loading');
            }
        }
    });    
  }  
})